package com.gxa.controller.zhy;

import com.gxa.common.R;
import com.gxa.common.ResponseData;
import com.gxa.common.jwt.JwtUtils;
import com.gxa.pojo.Performance;

import com.gxa.pojo.Permission;
import com.gxa.pojo.Role;
import com.gxa.pojo.Users;
import com.gxa.pojo.zhy.Pr;
import com.gxa.service.Impl.LoginServiceImpl;
import com.gxa.service.LoginService;
import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: 赵贺益
 * Date: 2021/12/02 14:31
 * Description:
 * Version: V1.0
 */
@Controller
@RequestMapping("/login")
@Api(value = "获取验证嘛和登录接口",tags = "登录界面的接口")
public class LoginController {
    public  static String vercodes;
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/queryVercode",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取验证码接口",notes = "获取到的就是一个对象",httpMethod = "GET", response = R.class)
    public R queryVercode(HttpSession session){
        R r=new R();
        SpecCaptcha specCaptcha = new SpecCaptcha(120,40,4);
        String vercode = specCaptcha.text().toLowerCase();
        this.vercodes=vercode;
        System.out.println(vercode);
//        HttpSession httpSession = request.getSession();
//        httpSession.setAttribute("serverVercode",vercode);
        session.setAttribute("serverVercode",vercode);
        Map map = new HashMap<>();
        map.put("image",specCaptcha.toBase64());
        System.out.println(specCaptcha.toBase64());
        r.setData(map);
        return r;
    }


    @RequestMapping(value ="verifyUserAndVercode" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "登录接口",notes = "获取到的就是一个对象",httpMethod = "POST", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "登录的账号" ,dataType = "String",required = true,paramType = "query"),
            @ApiImplicitParam(name = "user_password",value = "登录账号的密码",dataType = "String",required =true,paramType ="query"),
            @ApiImplicitParam(name = "vercode",value = "输入的验证码",dataType = "String",required = true,paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=200,message = "登录成功"),
            @ApiResponse(code =401,message = "验证码错误"),
            @ApiResponse(code=501,message = "账号或密码错误"),
            @ApiResponse(code=502,message = "该账号已经被冻结"),
            @ApiResponse(code = 500,message = "后台服务错误")
    })
//    public  ResponseData queryUser(@RequestBody Users users, HttpServletRequest request){
    public  ResponseData queryUser(@RequestBody Users users, HttpSession session){
        System.out.println("获取到的"+users.getUsername());
        System.out.println("获取到的"+users.getUser_password());
        System.out.println("获取到的"+users.getVercode());
        String username=users.getUsername();
        String user_password=users.getUser_password();
        String vercode=users.getVercode();
//

//        String user_password="123456";
        ResponseData rd=new ResponseData();

//        if(!vercode.equalsIgnoreCase(session.getAttribute("serverVercode").toString())){
        if(!vercode.equalsIgnoreCase(this.vercodes)){
//        if(!vercode.equalsIgnoreCase("1111")){
            rd.setCode(401);
            rd.setMsg("验证码错误");
            return rd;

        }else {
//            Users users=loginService.queryUserBy(username,user_password);
            users=loginService.queryUserBy(username,user_password);
            if(users!=null){
                if(users.getStatu()==0){
                    rd.setCode(501);
                    rd.setMsg("该账号已经被冻结");
                    return rd;
                }
                else {
                    /**
                     * 根据登录账号获取该用户对应的权限角色
                     */
                    Role role=loginService.queryRoleNameByusername(username);
                    /**
                     *获取该用户所有的可以操作的二级菜单
                     */
                    List<Permission> permissionList=loginService.queryPermissionByUsername(username);
                    List list= new ArrayList<>();
                    list.add(role);
                    list.add(permissionList);
                    rd.setData(list);
                    rd.setCode(200);
                    rd.setMsg("登录成功");
                    /**
                     * 发放token
                     */
                    String uuid= UUID.randomUUID().toString().replaceAll("-","");
                    String token= JwtUtils.createJwt(uuid,"gxa",username,JwtUtils.TTMILLIS);
                    rd.setToken(token);
                    return rd;
                }
            }
            else {
                rd.setCode(501);
                rd.setMsg("账号或密码错误");
                return rd;
            }

        }

    }




}
